package com.tripspins.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tripspins.app.MapActivity;
import com.tripspins.app.R;
import com.tripspins.app.bo.Pin;
import com.tripspins.app.bo.Trip;
import com.tripspins.app.interfaces.OnFragmentInteractionListener;
import com.facebook.UiLifecycleHelper;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by joaortk on 02/08/13.
 */
public class PinListAdapter  extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater = null;
    private UiLifecycleHelper uiLifecycleHelper;
    private Trip trip;
    OnFragmentInteractionListener listener;
    public PinListAdapter(Activity a, Trip trip, OnFragmentInteractionListener listener)
    {
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.trip = trip;
        this.listener = listener;
    }
    private static class ViewHolder
    {
        public int position;
        public TextView pinRowTitle;
        public TextView pinRowDate;
        public TextView pinRowLocation;
        public TextView pinRowAttCount;
        public TextView pinRowAuxId;
        public ImageView pinRowWeatherIcon;
        public RelativeLayout pinRow;
        public ImageView pinRowImgBackground;
    }

    @Override
    public int getCount() {
        return trip.getPins().size();
    }

    @Override
    public Object getItem(int i) {
        return trip.getPins().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        View v = view;
        if(view == null) {
            v = inflater.inflate(R.layout.pin_list_row, null);
            ViewHolder viewHolder = new ViewHolder();

            viewHolder.pinRowTitle = (TextView)v.findViewById(R.id.pin_row_title);
            viewHolder.pinRowDate = (TextView)v.findViewById(R.id.pin_row_date);
            viewHolder.pinRowLocation = (TextView)v.findViewById(R.id.pin_row_location);
            viewHolder.pinRowAttCount = (TextView)v.findViewById(R.id.pin_row_att_count);
            viewHolder.pinRowAuxId = (TextView)v.findViewById(R.id.pin_row_id_aux);
            //viewHolder.pinRowWeatherIcon = (ImageView)v.findViewById(R.id.pin_row_weather_icon);
            viewHolder.pinRow = (RelativeLayout)v.findViewById(R.id.pin_row);
            viewHolder.pinRowImgBackground = (ImageView)v.findViewById(R.id.pin_row_img);
            v.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder)v.getTag();
        v.setClickable(true);
        v.setFocusable(true);
        holder.position = i;
        holder.pinRowTitle.setText(trip.getPins().get(i).getTitle());
        holder.pinRowDate.setText(trip.getPins().get(i).getDateFormatted());
        holder.pinRowLocation.setText((trip.getPins().get(i).getAddress()==null? "-" : trip.getPins().get(i).getAddressString()));
        holder.pinRowAttCount.setText(String.valueOf(trip.getPins().get(i).getAttachments().size()));
        holder.pinRowAuxId.setText(String.valueOf(i));
        holder.pinRowImgBackground.setImageBitmap(null);
        // File weatherIcon =pins.get(i).getWeatherIconUri(activity,tripId);
        //holder.pinRowImgBackground.setImageURI(pins.get(i).getRandomImageUri());
        //holder.pinRowImgBackground.setImageURI(pins.get(i).getFirstImageUri());
        //holder.pinRowImgBackground.setHorizontalFadingEdgeEnabled(true);

        //if(weatherIcon!=null)
        //{
        // holder.pinRowWeatherIcon.setImageURI(Uri.fromFile(weatherIcon));
        //}


        try {
            new AsyncTask<ViewHolder, Void, Bitmap>() {
                private ViewHolder viewHolder;

                @Override
                protected Bitmap doInBackground(ViewHolder... params) {
                    viewHolder = params[0];
                    try {
                        return BitmapFactory.decodeFile(trip.getPins().get(i).getFirstImageString());
                    } catch (Throwable e) {
                        try {
                            System.gc();
                            return BitmapFactory.decodeFile(trip.getPins().get(i).getFirstImageString());
                        } catch (Throwable e2) {

                        }
                    }
                    return null;

                }

                @Override
                protected void onPostExecute(Bitmap bmp) {
                    super.onPostExecute(bmp);
                    if (viewHolder.position == i)
                        viewHolder.pinRowImgBackground.setImageBitmap(bmp);

                }
            }.execute(holder);
        }catch(Exception e){
            Log.e("TP",e.getMessage());}
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = Integer.parseInt(((TextView) view.findViewById(R.id.pin_row_id_aux)).getText().toString());
                listener.viewPinOnMap(trip.getPins().get(index), trip.getId());
            }
        });
        return v;
    }
}
